import { defineStore } from "pinia"
import { onMounted, ref } from "vue"
import { get, post } from "@/utils/api"
import { MessagePlugin } from "tdesign-vue-next"

export const useAdminEmailStore = defineStore("adminEmail", () => {
  const common = {
    saveConfig: async () => {
      try {
        const response = await post("/api/email/saveConfig", email.formData.value)
        MessagePlugin(response.status, response.message)
        await email.getConfig()
      } catch (err) {
        console.log(err)
      }
    }
  }
  const email = {
    formData: ref({
      id: 1,
      host: "",
      port: "",
      user: "",
      pass: ""
    }),
    getConfig: async () => {
      try {
        const config = await get("/api/email/getConfig")
        config.port = config.port.toString()
        email.formData.value = config
      } catch (err) {
        console.log(err)
      }
    },
  }
  onMounted(async () => {
    await email.getConfig()
  })

  return {
    common,
    email
  }
})
